<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Admin;


use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\AdminLog;
use App\Models\Brand;
use App\Models\Good;
use App\Models\GoodsOneType;
use App\Models\GoodsTwoType;
use App\Models\IntegralGood;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class AdminController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Admin\AdminController";

    public function index(Request $request)
    {
        $phone = $request->phone;
        $userName = $request->userName;
        $nickname = $request->nickname;
        $position = $request->position;
        $status = $request->status;
        $pageSize = $request->pageSize;
        $sql = Admin::select('id', 'userName', 'nickname', 'phone', 'position', 'positionDesc', 'createTime', 'status')
            ->where('isDel',0);
        if (isset($status)) {
            $sql->where('status',$status);
        }
        if (isset($userName)) {
            $sql->where('userName', 'like', "%$userName%");
        }
        if (isset($phone)) {
            $sql->where('phone', 'like', "%$phone%");
        }
        if (isset($nickname)) {
            $sql->where('nickname', 'like', "%$nickname%");
        }
        if (isset($position)) {
            $sql->where('position', 'like', "%$position%");
        }
        $adminList = $sql->orderByDesc('createTime')
            ->paginate($pageSize)
            ->toArray();
        $data['adminList'] = $adminList['data'];
        $data['totalNum'] = $adminList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $admin = Admin::select('id', 'userName', 'nickname', 'phone', 'position', 'positionDesc', 'resource')->where('id', $id)->first();
        return $this->apiResponse($admin);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('userName', 'nickname', 'phone', 'position', 'positionDesc','resource');
        $password = $request->password;
        if (!isset($data['userName']) || !isset($data['phone'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        if(!isset($data['nickname'])){
            $data['nickname'] = $data['userName'];
        }
        $time = date("Y-m-d H:i:s");
        $data['updateTime'] = $time;
        $num = Admin::where('userName', $data['userName'])->where('id','!=',$id)->where('isDel',0)->count();
        if($num){
            return $this->apiResponse('', config('errorLiu.YIOCUNZAIGAIIDMINGMIYONG'));
        }
        $num = Admin::where('phone', $data['phone'])->where('id','!=',$id)->where('isDel',0)->count();
        if($num){
            return $this->apiResponse('', config('errorLiu.YICUNAZAIIHFGAIHADIANHAUD'));
        }
        if ($id) {
            $content = "修改";
            Admin::where('id', $id)->update($data);
        } else {
            $content = "新增";
            $data['createTime'] = $time;
            $id = Admin::insertGetId($data);
        }
        $additionalContent = null;
        if(isset($password) && !empty($password)){
            $salt = "liuliuliu".$id;
            $password = md5($salt.md5($password));
            Admin::where('id', $id)->update([
                'password'=>$password,
                'accessToken'=>null
            ]);
            $additionalContent = ",本次修改涉及密码修改";
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的管理员,内容为【".json_encode($data)."】".$additionalContent;
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "管理员模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $admin = Admin::select('id', 'userName', 'nickname', 'phone', 'position', 'positionDesc', 'resource')->where('id', $id)->first();
        Admin::where('id', $id)->update([
            'accessToken'=>null,
            'isDel' => 1,
            'status' => 0
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了管理员的内容为【".json_encode($admin)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "管理员模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function log(Request $request)
    {
        $id = $request->id;
        $phone = $request->phone;
        $userName = $request->userName;
        $nickname = $request->nickname;
        $position = $request->position;
        $title = $request->title;
        $model = $request->model;
        $createStartTime = $request->createStartTime;
        $createEndTime = $request->createEndTime;
        $pageSize = $request->pageSize;
        $sql = AdminLog::select('t1.id','t2.id as adminId','t2.userName','t2.nickname','t2.phone','t2.position','t2.positionDesc','t1.title','t1.model','t1.content','t1.createTime','t1.ip')
            ->from((new AdminLog())->getTable().' as t1')
            ->leftJoin((new Admin())->getTable().' as t2','t1.adminId','=','t2.id');
        if (isset($id)) {
            $sql->where('t2.id',$id);
        }
        if (isset($phone)) {
            $sql->where('t2.phone', 'like', "%$phone%");
        }
        if (isset($userName)) {
            $sql->where('t2.userName', 'like', "%$userName%");
        }
        if (isset($nickname)) {
            $sql->where('t2.nickname', 'like', "%$nickname%");
        }
        if (isset($position)) {
            $sql->where('t2.position', 'like', "%$position%");
        }
        if (isset($title)) {
            $sql->where('t1.title', 'like', "%$title%");
        }
        if (isset($model)) {
            $sql->where('t1.model', 'like', "%$model%");
        }
        if (isset($createStartTime)) {
            $sql->where('t1.createTime', '>=', $createStartTime);
        }
        if (isset($createEndTime)) {
            $sql->where('t1.createTime', '<=', $createEndTime);
        }
        $adminLogList = $sql->orderByDesc('t1.createTime')
            ->paginate($pageSize)
            ->toArray();
        $data['adminLogList'] = $adminLogList['data'];
        foreach ($data['adminLogList'] as $key => $value) {
            if (isset($value['ip'])) {
                $data['adminLogList'][$key]['ip'] = ip2area($value['ip']);
            }
        }
        $data['totalNum'] = $adminLogList['total'];
        //添加管理员日志开始
        $url = $this->namespace . "/log";
        $title = "查看";
        $content = "这个人偷偷摸摸看操作日志,被乐水抓到了QAQ";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "管理员模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse($data);
    }
}